package com.wlld.tools;

import com.alibaba.fastjson.JSONObject;
import com.wlld.bean.BeanMangerOnly;
import com.wlld.config.Config;
import com.wlld.entity.Sentence;
import org.wlld.entity.KeyWordForSentence;
import org.wlld.naturalLanguage.languageCreator.CatchKeyWord;
import org.wlld.naturalLanguage.languageCreator.CreatorSentenceModel;
import org.wlld.naturalLanguage.languageCreator.KeyWordModel;
import org.wlld.naturalLanguage.languageCreator.SentenceCreator;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Tools {
    private CreatorSentenceModel readCreatorSentenceModel(BeanMangerOnly beanMangerOnly, List<String> sentenceList) throws Exception {
        File file = new File(Config.SentenceModelUrl); //创建文件
        CreatorSentenceModel model = null;
        if (file.exists()) {//模型文件存在
            model = JSONObject.parseObject(readPaper(file), CreatorSentenceModel.class);
        } else {//模型文件不存在进行训练
            SentenceCreator creator = beanMangerOnly.sentenceCreator();
            creator.initFirst(sentenceList, beanMangerOnly.temple());
            creator.study();//非常耗时的过程
            String myModel = JSONObject.toJSONString(creator.getModel());
            writeModel(myModel, Config.SentenceModelUrl);//训练结束保存模型文件
        }
        return model;
    }

    private void keyWord(BeanMangerOnly beanMangerOnly, List<Sentence> sentences) throws IOException {//处理关键词
        File file = new File(Config.KeyWordModelUrl); //创建文件
        CatchKeyWord catchKeyWord = beanMangerOnly.catchKeyWord();
        if (!file.exists()) {//重新学习
            List<KeyWordForSentence> keyWordForSentenceList = new ArrayList<>();
            for (Sentence sentence : sentences) {
                String word = sentence.getWord();
                String keyWord = sentence.getNoun();//关键词
                if (keyWord != null) {
                    KeyWordForSentence keyWordForSentence = new KeyWordForSentence();
                    keyWordForSentence.setSentence(word);
                    keyWordForSentence.setKeyWord(keyWord);
                    keyWordForSentenceList.add(keyWordForSentence);
                }
            }
            catchKeyWord.study(keyWordForSentenceList);//耗时的过程
            KeyWordModel keyWordModel = catchKeyWord.getModel();
            //模型写出
            writeModel(JSONObject.toJSONString(keyWordModel), Config.KeyWordModelUrl);
        } else {//读取模型
            KeyWordModel keyWordModel = JSONObject.parseObject(readPaper(file), KeyWordModel.class);
            catchKeyWord.insertModel(keyWordModel);
        }
    }

    public void initSemantics(BeanMangerOnly beanMangerOnly, List<Sentence> sentences) throws Exception {
        Map<Integer, List<String>> model = new HashMap<>();//语义识别
        List<String> sentenceList = new ArrayList<>();//语句延伸 与 关键词抓取
        for (Sentence sentence : sentences) {
            int type = sentence.getSecond();
            String word = sentence.getWord();
            sentenceList.add(word);
            if (model.containsKey(type)) {
                model.get(type).add(word);
            } else {
                List<String> words = new ArrayList<>();
                words.add(word);
                model.put(type, words);
            }
        }
        beanMangerOnly.tokenizer().start(model);//拆词及rf学习
        keyWord(beanMangerOnly, sentences);//读取关键词模型
        CreatorSentenceModel creatorModel = readCreatorSentenceModel(beanMangerOnly, sentenceList);
        if (creatorModel != null) {//有模型
            beanMangerOnly.sentenceCreator().initModel(beanMangerOnly.temple(), creatorModel);//生成词句模型
        }

    }

    private void writeModel(String model, String url) throws IOException {//写出模型与 激活参数
        BufferedWriter out = new BufferedWriter(new FileWriter(url));
        out.write(model);
        out.close();
    }

    private String readPaper(File file) {
        Reader read = null;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //一次读一个字符
            read = new InputStreamReader(new FileInputStream(file));
            int tempchar;
            while ((tempchar = read.read()) != -1) {
                stringBuilder.append((char) tempchar);
            }
            read.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (read != null) {
                try {
                    read.close(); //确保关闭
                } catch (IOException el) {
                }
            }
        }
        return stringBuilder.toString();
    }
}
